home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Boxer / Atache / atache.c next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  9.5 KB  |  438 lines

  1. #define IGNORE_STDIO_STUBS
  2. #define __string_h
  3.  
  4. #include <PalmOS.h>
  5. #include <PalmCompatibility.h>
  6.  
  7. #include <Unix/sys_socket.h>
  8.  
  9. #include "stringil.h"
  10. #include "stdio2.h"
  11.  
  12. #ifdef __PALMOS_TRAPS__
  13. Err errno;
  14. #endif
  15.  
  16. struct prchead {
  17.   char name[32];                //0-32
  18.   short int attr;               //32-33
  19.   short int vers;               //34-35
  20.   long int cr, md, bkt;         //times 36-47
  21.   long int mn, app, sort;       // zero 48-59 - zero for prcs.
  22.   long int type, crea;          //60-67
  23.   long int uidseed, nxrec;      //68-75 - uidseed rand, nxrec zero;
  24.   short int nrecs;              //76-78
  25. };
  26.  
  27. struct rsrcdbent {
  28.   long int rsrc;
  29.   short int rsid;
  30.   long int ofst;                //80+10*(nrecs+index)
  31. };
  32.  
  33. struct normdbent {
  34.   long int ofst;                //80+8*(nrecs+index)
  35.   unsigned char attr, uid[3];
  36. };
  37.  
  38. #define wrtit(buf,len,fd) send(fd, buf, len, 0)
  39.  
  40. void SendPrc(UInt16 cardno, LocalID lid, unsigned int fd)
  41. {
  42.   struct prchead head;
  43.   struct rsrcdbent rsrcent;
  44.   struct normdbent dataent;
  45.   unsigned char buf[40];
  46.   LocalID aiid, siid, lid2;
  47.   int i;
  48.   UInt16 attr;
  49.   UInt32 uid, asz, ssz, ofst, t;
  50.   DmOpenRef db;
  51.   void *ap, *sp, *xp;
  52.  
  53.   memset(&head, 0, sizeof(head));
  54.  
  55.   DmDatabaseInfo(cardno, lid, buf, &head.attr, &head.vers, &head.cr, &head.md,
  56.                  &head.bkt, &head.mn, &aiid, &siid, &head.type, &head.crea);
  57.  
  58.   strncpy(head.name, buf, 32);
  59.   WinDrawChars(buf, strlen(buf), 0, 15);
  60.  
  61.   asz = 0;
  62.   ssz = 0;
  63.   head.app = 0;
  64.   head.sort = 0;
  65.  
  66.   db = DmOpenDatabase(cardno, lid, dmModeReadOnly);
  67.  
  68.   if (aiid) {
  69.     ap = MemLocalIDToLockedPtr(aiid, cardno);
  70.     asz = MemPtrSize(ap);
  71.   }
  72.   if (siid) {
  73.     sp = MemLocalIDToLockedPtr(siid, cardno);
  74.     ssz = MemPtrSize(sp);
  75.   }
  76.   if (head.attr & dmHdrAttrResDB) {
  77.  
  78.     head.nrecs = DmNumResources(db);
  79.     ofst = 80 + 10 * head.nrecs;
  80.  
  81.     if (asz)
  82.       head.app = ofst;
  83.     if (ssz)
  84.       head.sort = ofst + asz;
  85.     ofst += asz + ssz;
  86.     wrtit(&head, sizeof(head), fd);
  87.  
  88.     for (i = 0; i < head.nrecs; i++) {
  89.  
  90.       DmResourceInfo(db, i, &rsrcent.rsrc, &rsrcent.rsid, &lid2);
  91.       rsrcent.ofst = ofst;
  92.  
  93.       xp = DmGetResourceIndex(db, i);
  94.       uid = MemHandleSize(xp);
  95.       DmReleaseResource(xp);
  96.       ofst += uid;
  97.  
  98.       wrtit(&rsrcent, sizeof(rsrcent), fd);
  99.     }
  100.     uid = 0;
  101.     wrtit(&uid, 2, fd);
  102.  
  103.     if (aiid) {
  104.       wrtit(ap, asz, fd);
  105.       MemPtrUnlock(ap);
  106.     }
  107.     if (siid) {
  108.       wrtit(sp, ssz, fd);
  109.       MemPtrUnlock(sp);
  110.     }
  111.     for (i = 0; i < head.nrecs; i++) {
  112.       ap = DmGetResourceIndex(db, i);
  113.       sp = MemHandleLock(ap);
  114.       uid = MemPtrSize(sp);
  115.  
  116.       ofst = uid;
  117.       while (ofst) {
  118. #define WRTLIM 2048
  119.         t = wrtit(sp, ofst > WRTLIM ? WRTLIM : ofst, fd);
  120.         sp += t;
  121.         ofst -= t;
  122.       }
  123.  
  124.       MemHandleUnlock(ap);
  125.       DmReleaseResource(ap);
  126.     }
  127.  
  128.   } else {
  129.  
  130.     head.nrecs = DmNumRecords(db);
  131.     ofst = 80 + 8 * head.nrecs;
  132.  
  133.     if (asz)
  134.       head.app = ofst;
  135.     if (ssz)
  136.       head.sort = ofst + asz;
  137.     ofst += asz + ssz;
  138.     wrtit(&head, sizeof(head), fd);
  139.  
  140.     for (i = 0; i < head.nrecs; i++) {
  141.  
  142.       DmRecordInfo(db, i, &attr, &uid, &lid2);
  143.       dataent.attr = attr;
  144.       dataent.uid[0] = uid >> 16;
  145.       dataent.uid[1] = uid >> 8;
  146.       dataent.uid[2] = uid;
  147.       dataent.ofst = ofst;
  148.  
  149.       xp = DmGetRecord(db, i);
  150.       uid = MemHandleSize(xp);
  151.       DmReleaseRecord(db, i, false);
  152.       ofst += uid;
  153.       wrtit(&dataent, sizeof(dataent), fd);
  154.     }
  155.     i = 0;
  156.     wrtit(&i, 2, fd);
  157.  
  158.     if (aiid)
  159.       wrtit(ap, asz, fd);
  160.  
  161.     if (siid)
  162.       wrtit(sp, ssz, fd);
  163.  
  164.     for (i = 0; i < head.nrecs; i++) {
  165.       ap = DmGetRecord(db, i);
  166.       sp = MemHandleLock(ap);
  167.       uid = MemPtrSize(sp);
  168.  
  169.       ofst = uid;
  170.       while (ofst) {
  171.         t = wrtit(sp, ofst > WRTLIM ? WRTLIM : ofst, fd);
  172.         sp += t;
  173.         ofst -= t;
  174.       }
  175.  
  176.       MemHandleUnlock(ap);
  177.       DmReleaseRecord(db, i, false);
  178.     }
  179.  
  180.   }
  181.  
  182.   DmCloseDatabase(db);
  183.  
  184. }
  185.  
  186. char httphead1[] =
  187.   "HTTP/1.1 200 OK\nConnection: close\nContent-type: application/octet-stream\n\n";
  188.  
  189. char httpheadh[] =
  190.   "HTTP/1.1 200 OK\nConnection: close\nContent-type: text/html\n\n";
  191.  
  192. char httpheadt[] =
  193.   "HTTP/1.1 200 OK\nConnection: close\nContent-type: text/plain\n\n";
  194.  
  195. char httpheadg[] =
  196.   "HTTP/1.1 200 OK\nConnection: close\nContent-type: image/gif\n\n";
  197.  
  198. char htmlhead[] =
  199.   "<html><head><title>MyOmniSkyPalmV</title></head><body><table>\n";
  200. char htmlmid[] = "<tr><th>Database<th>Type<th>Crea<th>NRec<th>Size<th>Attr\n";
  201. char htmltail[] = "\n</table></body></html>\n";
  202.  
  203.  
  204. DWord PilotMain(Word cmd, Ptr cmdPBP, Word launchFlags)
  205. {
  206.   unsigned char *buf;
  207.   NetSocketAddrType addr;
  208.  
  209.   NetSocketRef fd, fd0;
  210.   int endit;
  211.   Int16 addrlen;
  212.   NetSocketAddrINType *naddr;
  213.   UInt16 cardno;
  214.  
  215.   Err err, err2;
  216.   Byte allup;
  217.   LocalID lid;
  218.   int i;
  219.  
  220.   if (cmd != sysAppLaunchCmdNormalLaunch)
  221.     return 0;
  222.  
  223.   fd = -1, fd0 = -1;
  224.   endit = 0;
  225.   addrlen = sizeof(addr);
  226.   naddr = &addr;
  227.   cardno = 0;
  228.   buf = MemPtrNew(512);
  229.  
  230.   naddr->family = PF_INET;
  231.   naddr->port = 80;
  232.   naddr->addr = 0UL;
  233.  
  234.   AppNetRefnum = 0;
  235.  
  236.   err = SysLibFind("Net.lib", &AppNetRefnum);
  237.   err |= NetLibOpen(AppNetRefnum, &err2);
  238.   NetLibConnectionRefresh(AppNetRefnum, true, &allup, &err2);
  239.  
  240.   //  if (err | err2)
  241.   //  return 1;
  242.  
  243.   AppNetTimeout = 1000;
  244.  
  245.   fd0 = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  246.  
  247.   if (fd0 < 0)
  248.     return 1;
  249.  
  250.   bind(fd0, &addr, addrlen);
  251.   fd = listen(fd0, 1);
  252.   if (fd != 0)
  253.     return 1;
  254.  
  255.   do {
  256.  
  257.     fd = -1;
  258.     AppNetTimeout = 50;
  259.  
  260.     for (;;) {
  261.       WinDrawChars("Wait      ", 10, 0, 15);
  262.       {
  263.         EventType e;
  264.         do {
  265.           EvtGetEvent(&e, 0);
  266.           SysHandleEvent(&e);
  267.           if (e.eType == appStopEvent)
  268.             endit = 1;
  269.         } while (e.eType != nilEvent);
  270.       }
  271.       if (endit) {
  272.         WinDrawChars("Exit      ", 10, 0, 15);
  273.         break;
  274.       }
  275.       fd = accept(fd0, &addr, &addrlen);
  276.  
  277.       WinDrawChars("Acpt      ", 10, 0, 15);
  278.  
  279.       if (fd >= 0)
  280.         break;
  281.  
  282.     }
  283.  
  284.     AppNetTimeout = -1L;
  285.  
  286.     if (fd < 0)
  287.       break;
  288.  
  289.     WinDrawChars("Connect   ", 10, 0, 15);
  290.  
  291.     recv(fd, buf, 512, 0);
  292.  
  293.     WinDrawChars(&buf[5], 80, 0, 0);
  294.  
  295.     for (i = 5; i < 40; i++)
  296.       if (!strncmp(&buf[i], " HTTP/1.", 8)) {
  297.         buf[i] = 0;
  298.         if (!strcmp(&buf[i - 4], ".prc"))
  299.           buf[i - 4] = 0;
  300.         if (!strcmp(&buf[i - 4], ".pdb"))
  301.           buf[i - 4] = 0;
  302.         break;
  303.       }
  304.     WinDrawChars(&buf[5], strlen(&buf[5]), 0, 30);
  305.  
  306.     if (buf[5]) {
  307.  
  308.       if (buf[5] == '0' && !buf[6])
  309.         endit = 1;
  310.  
  311.       else {
  312.  
  313.  
  314.         if ((lid = DmFindDatabase(cardno, &buf[5]))) {
  315.           UInt16 attr;
  316.           DmDatabaseInfo(cardno, lid, NULL, &attr, NULL, NULL, NULL, NULL, NULL,
  317.                          NULL, NULL, NULL, NULL);
  318.  
  319.           if (!(attr & dmHdrAttrStream)) {
  320.         send(fd, httphead1, strlen(httphead1), 0);
  321.             SendPrc(0, lid, fd);
  322.       }
  323.           else {
  324.             FILE *ifd;
  325.             unsigned char ibuf[WRTLIM];
  326.             int ic, oc, t;
  327.  
  328.         if (!strcmp(&buf[i - 4], ".gif"))
  329.           send(fd, httpheadg, strlen(httpheadg), 0);
  330.         if (!strcmp(&buf[i - 5], ".html"))
  331.           send(fd, httpheadh, strlen(httpheadh), 0);
  332.         if (!strcmp(&buf[i - 4], ".txt"))
  333.           send(fd, httpheadt, strlen(httpheadt), 0);
  334. #if 0
  335.         if (!strncmp(buf, "cgi-bin/",8)){
  336.           lid = DmFindDatabase(0, &buf[8]);
  337.           SysUIAppSwitch(0, lid, 0, NULL);
  338.           }
  339. #endif
  340.             ifd = fopen(&buf[5], "r");
  341.  
  342.             while (!feof(ifd)) {
  343.               ic = fread(ibuf, 1, WRTLIM, ifd);
  344.               if (ic <= 0)
  345.                 break;
  346.               oc = 0;
  347.               while (oc < ic) {
  348.                 t = wrtit(&ibuf[oc], ic - oc, fd);
  349.                 if (t < 0)
  350.                   break;
  351.                 oc += t;
  352.               }
  353.             }
  354.  
  355.             fclose(ifd);
  356.  
  357.           }
  358.  
  359.         }
  360.       }
  361.     } else {
  362.       send(fd, htmlhead, strlen(htmlhead), 0);
  363.  
  364.       send(fd, htmlmid, strlen(htmlmid), 0);
  365.  
  366.  
  367.       {
  368.         LocalID lid, aiid, siid;
  369.         int i;
  370.         UInt16 cardno = 0;
  371.         //        DateTimeType dt;
  372.         char name[36];
  373.  
  374.         UInt16 num, attr, vers;
  375.         UInt32 cr, md, bk, type, crea, mn, nrec, sz;
  376.         num = DmNumDatabases(cardno);
  377.  
  378.         for (i = 0; i < num; i++) {
  379.           lid = DmGetDatabase(cardno, i);
  380.  
  381.           DmDatabaseInfo(cardno, lid, name, &attr, &vers, &cr, &md, &bk, &mn,
  382.                          &aiid, &siid, &type, &crea);
  383.  
  384.           memset(buf, 0, 256);
  385.           strcpy(buf, "\n<tr><td><a HREF=\"");
  386.  
  387.           strcat(buf, name);
  388.  
  389.           if (attr & dmHdrAttrResDB)
  390.             strcat(buf, ".prc");
  391.           else if (!(attr & (dmHdrAttrLaunchableData | dmHdrAttrStream))) //PQAs
  392.  
  393.             strcat(buf, ".pdb");
  394.  
  395.           strcat(buf, "\">");
  396.           strcat(buf, name);
  397.           strcat(buf, "</a><td>");
  398.           strncat(buf, &type, 4);
  399.           strcat(buf, "<td>");
  400.           strncat(buf, &crea, 4);
  401.           strcat(buf, "<td>");
  402.  
  403.           DmDatabaseSize(cardno, lid, &nrec, &sz, NULL);
  404.           StrIToA(name, nrec);
  405.           strcat(buf, name);
  406.           strcat(buf, "<td>");
  407.           StrIToA(name, sz);
  408.           strcat(buf, name);
  409.           strcat(buf, "<td>");
  410.           StrIToH(name, attr);
  411.           strcat(buf, &name[4]);
  412.           send(fd, buf, strlen(buf), 0);
  413. #if 0
  414.           StrIToH(buf, vers);
  415.           send(fd, &buf[4], 4, 0);
  416.           //      send(fd, "<td>",4 , 0);
  417.           //      TimSecondsToDateTime(cr, &dt);
  418. #endif
  419.  
  420.         }
  421.       }
  422.       send(fd, htmltail, strlen(htmltail), 0);
  423.     }
  424.  
  425.     WinDrawChars("DONE      ", 10, 0, 15);
  426.     close(fd);
  427.   }
  428.   while (!endit);
  429.  
  430.   if (fd0 != -1)
  431.     close(fd0);
  432.  
  433.   err = NetLibClose(AppNetRefnum, true);
  434.   SysTaskDelay(200);
  435.  
  436.   return 0;
  437. }
  438.